angular.module('CvirtFilters', []).filter('bytes', function() {
  var kilobytes = 1024, megabytes = kilobytes * 1024, gigabytes = megabytes * 1024, 
    terabytes = gigabytes * 1024, petabytes = terabytes * 1024, exabytes = petabytes * 1024;

    function round(num){
        return Math.round(num*10)/10;  //保留一位小数
    }
  return function(num){
    if(num > exabytes){
      return "" + round(num / exabytes) + "EB";
    }else if(num > petabytes){
      return "" + round(num / petabytes) + "PB";
    }else if(num > terabytes){
      return "" + round(num / terabytes) + "TB";
    }else if(num > gigabytes){
      return "" + round(num / gigabytes) + "GB";
    }else if(num > megabytes){
      return "" + round(num / megabytes) + "MB";
    }else if(num > kilobytes){
      return "" + round(num / kilobytes) + "KB";
    } else { 
      return "" + num + "B"
    }
  };
}).filter('startFrom', function() {
  return function(input, start) {
    input || (input = []);
    start = +start; //parse to int
    return input.slice(start);
  }
}).filter('range', function(){
  return function(input, total){
    for(var i = 0; i < total; i++){
      input.push(i);
    }
    return input;
  };
}).filter('round', function(){
  return function(input){
      if(!input) return 0;
    return Math.round(input);
  };
}).filter('backuptype', function(){
    return function(type) {
        return type == 0 ? "冷备份" : "热备份";
    }
}).filter('backuplocation', function(){
    return function(backupstrategy, param) {
        if(backupstrategy){
            if (backupstrategy.locationtype == 0)
                return backupstrategy.localpath;
            else if (backupstrategy.locationtype == 1) {
                return backupstrategy.storage.name;
            }
            else if (backupstrategy.locationtype == 3)
                return backupstrategy.sftphost + "/" + backupstrategy.sftppath;
        }
    }
}).filter('backuptime', function() {

    return function(backupstrategy) {
        if(backupstrategy){
            var result = "";
            if (backupstrategy.backupfrequency == 0) {
                result = "每小时 " + backupstrategy.backuptime;
            } else if (backupstrategy.backupfrequency == 1) {
                result = "每天 " + backupstrategy.backuptime;
            } else if (backupstrategy.backupfrequency == 2) {
                result = "每周" + backupstrategy.weekday + " " + backupstrategy.backuptime;
            } else if (backupstrategy.backupfrequency == 3) {
                result = "每月" + backupstrategy.monthday + "号 " + backupstrategy.backuptime;
            }

            return result;
        }

    }
}).filter('security_direction', function() {

    return function(security) {

        if (security.direction == 'in') {
            return "外部 -> VM";
        } else if (security.direction == 'out') {
            return "VM -> 外部";
        } else if (security.direction == 'inout') {
            return "VM <-> 外部";
        }
    }
}).filter('yesorno', function() {

    return function(d) {
        return d?"是":"否";
    }
}).filter('allocated', function() {

    return function(d) {

        if (d) {
            return "是";
        }
    }
}).filter('network_type', function() {

    return function(n) {

            switch(n)
            {
                case 0:
                    return 'NAT';
                case 1:
                    return '隔离';
                case 2:
                    return '桥接';
                case 3:
                    return 'VLAN';
                default:
                    return "";
            }
    }
}).filter('activeitem', function() {

    return function(n) {
        if(n){
            return $.grep(n,function(i){
                return !i.deleted&&!i.dettached;
            })
        }

    }
}).filter('vmstatus', function() {
    var state_map = {
        running:'运行中',
        shutdown :'已关闭',
        paused:'已暂停',
        saved:'已保存',
        failed:'异常',
        pending:'创建中'

    }
    return function(n) {
        return state_map[n];
    }
}).filter('hoststatus', function(){
    var state_map = {
        "0":"维护模式",
        "1":"未激活",
        "2":"已激活",
        "3":"已关机",
        "4":"操作中"
    }

    return function(n) {
        return state_map[n];
    }
}).filter('storagetype', function() {
    var state_map = {
        iso:'ISO',
        data :'数据'
    }
    return function(n) {
        return state_map[n];
    }
}).filter('activity_running', function() {
    return function(n) {
        if(n){
            return $.grep(n,function(i){
                return i.meta.status!='FAILURE'&&i.meta.status!='SUCCESS';
            })
        }
    }
}).filter('activity_failed', function() {
    return function(n) {
        if(n){
            return $.grep(n,function(i){
                return i.meta.status=='FAILURE';
            })
        }
    }
}).filter('userstatus', function() {
       return function(n) {

            switch(n)
            {
                case 'deny':
                    return '禁用';
                case 'online':
                    return '在线';
                case 'offline':
                    return '离线'
                default:
                    return "";
            }
    }
});
